import 'package:excel_utils/pages/week/bean/QuotationRecordBean.dart';
import 'package:excel_utils/pages/week/week_entry/child/quotation_record/bloc/quotation_record_cubit.dart';
import 'package:excel_utils/repository/week/quotation_record_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class QuotationDataPage extends StatefulWidget {
  const QuotationDataPage({super.key});

  @override
  State<QuotationDataPage> createState() => _QuotationDataPageState();
}

class _QuotationDataPageState extends State<QuotationDataPage> {
  String startDateStr = "2022-01-01";
  String endDateStr = "2022-12-31";

  List<QuotationRecordBean> quotationRecordList = [];

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: ((context) => QuotationRecordCubit(
            context.read<QuotationRecordRepository>(),
          )..getQuotationRecord(
              maps: {"startDate": "$startDateStr", "endDate": "$endDateStr"},
            )),
      child: BlocBuilder<QuotationRecordCubit, QuotationRecordState>(
        builder: (BuildContext context, QuotationRecordState state) {
          if (state is QuotationRecordDataLoading) {
            return const Center(child: CircularProgressIndicator());
          }
          if (state is QuotationRecordDataSuccessState) {
            quotationRecordList = state.quotationRecordData;
          }
          if (state is QuotationRecordDataFailureState) {
            return Center(child: Text(state.message));
          }

          return buildUI();
        },
      ),
    );
  }

  Widget buildUI() {
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Expanded(child: Center(child: Text("No."))),
              Expanded(child: Center(child: Text("報價日期"))),
              Expanded(child: Center(child: Text("負責客服"))),
              Expanded(child: Center(child: Text("客戶公司名稱"))),
              Expanded(child: Center(child: Text("客戶地區"))),
              Expanded(child: Center(child: Text("聯絡人"))),
              Expanded(child: Center(child: Text("電話"))),
              Expanded(child: Center(child: Text("如何得知本公司途徑"))),
              Expanded(child: Center(child: Text("想開月費 / 套票"))),
              Expanded(child: Center(child: Text("報了什麼價  (工商/偏遠/標準/優惠價)"))),
              Expanded(child: Center(child: Text("後續跟進結果(半天後詢問客戶, 也可將客戶反映意見告知)"))),
              Expanded(child: Center(child: Text("是否成功開戶"))),
              Expanded(child: Center(child: Text("数据归属日期范围"))),
            ],
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: quotationRecordList.length,
            itemBuilder: (BuildContext context, int index) {
              QuotationRecordBean quotationRecord = quotationRecordList[index];
              return Row(
                children: [
                  Expanded(child: Center(child: Text("${index + 1}"))),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.quotationDate}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.personCharge}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.customerCompanyName}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.customerRegion}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.contactPerson}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.phone}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.source}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.monthlyOrPackage}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.quotation}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.followUp}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.result}")),
                  SizedBox(width: 3),
                  Expanded(child: Text("${quotationRecord.dateRange}")),
                  SizedBox(width: 3),
                ],
              );
            },
          ),
        ],
      ),
    );
  }
}
